package com.miao.manage.web.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.miao.entity.PageVo;
import com.miao.entity.TbItemParam;
import com.miao.entity.TbItemParamItem;
import com.miao.service.TbItemParamService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Date;

/**
 * <p>
 * 商品规则参数 前端控制器
 * </p>
 *
 * @author shuangshuang
 * @since 2018-09-07
 */
@Controller
@RequestMapping("/tbItemParam")
public class TbItemParamController {
    //日志
    private static final Logger LOGGER = LoggerFactory.getLogger(TbItemCatController.class);

    @Autowired
    private TbItemParamService tbItemParamService;
    /**
     * 根据ID查询商品类目是否选中
     * @param itemCatId
     * @return
     */
    @RequestMapping(value = "{itemCatId}",method = RequestMethod.GET)
    public ResponseEntity<TbItemParam> queryItemParam(@PathVariable("itemCatId") Long itemCatId){
        try{
            TbItemParam tbItemParam = new TbItemParam();
            EntityWrapper<TbItemParam> wrapper = new EntityWrapper<>();
            wrapper.setSqlSelect("param_data","created","updated","item_cat_id");
            wrapper.setEntity(tbItemParam);
            wrapper.where("item_cat_id={0}",itemCatId);
            TbItemParam tbItemParam1 = tbItemParamService.selectOne(wrapper);
            if(null==tbItemParam1){
                //404
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            return ResponseEntity.ok(tbItemParam1);
        }catch (Exception e){
            LOGGER.error("查询失败！",e);
        }
        return ResponseEntity.status(HttpStatus.INSUFFICIENT_STORAGE).body(null);
    }


    /**
     * 添加规格参数
     * @return
     */
    @RequestMapping(value = "{itemCatId}",method = RequestMethod.POST)
    public  ResponseEntity<Void> additemparam(@PathVariable("itemCatId") Long itemCatId, @RequestParam("paramData") String  paramData){

        try{
            TbItemParam tbItemParam = new TbItemParam();
            tbItemParam.setId(null);
            tbItemParam.setParamData(paramData);
            tbItemParam.setItemCatId(itemCatId);
            tbItemParam.setCreated(new Date());
            tbItemParam.setUpdated(new Date());
            boolean insert = tbItemParamService.insert(tbItemParam);
            return ResponseEntity.status(HttpStatus.CREATED).build();
        }catch (Exception e){
            LOGGER.error("添加失败！",e);
        }
        return ResponseEntity.status(HttpStatus.INSUFFICIENT_STORAGE).build();
    }

    /**
     * 分页查询 默认为第一页
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<PageVo> query(@RequestParam(defaultValue = "1",name = "page") Integer pageNum, Integer rows) {
        Page<TbItemParam> tbItemPage = new Page<>(pageNum, rows);
        EntityWrapper<TbItemParam> wrapper = new EntityWrapper<>();
        wrapper.orderBy("created DESC");
        Page<TbItemParam> tbItemParamPage = tbItemParamService.selectPage(tbItemPage, wrapper);
        //EasyUI 分页插件
        PageVo<TbItemParam> pageVo = new PageVo<>();
        pageVo.setTotal((int)tbItemParamPage.getTotal());
        pageVo.setRows(tbItemParamPage.getRecords());
        return ResponseEntity.ok(pageVo);

    }

    /**
     * 删除商品规格
     * @return
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public  ResponseEntity<Void> itemparamdelet(String ids){
        try{
            EntityWrapper<TbItemParam> wrapper = new EntityWrapper<>();
            wrapper.where("id in("+ids+")");
            tbItemParamService.delete(wrapper);
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }catch (Exception e){
            LOGGER.error("删除失败！",e);
        }
        return ResponseEntity.status(HttpStatus.INSUFFICIENT_STORAGE).build();
    }
}

